<?php

  // Database connection settings
  define("PG_DB"  , "balkan_20081113_postgis");
  define("PG_HOST", "192.168.2.10");
  define("PG_USER", "postgres");
  define("PG_PORT", "5432");
  define("PG_PWD", "postgres123");


  $adminType = $_REQUEST["adminType"];
  $showIcons = $_REQUEST["showIcons"];

  //$adminType_9 = mb_convert_encoding($adminType, "UTF-8");

  //$encoding = mb_detect_encoding($adminType, 'UTF-8, ASCII, ISO-8859-9');
  //$encoding_9 = mb_detect_encoding($adminType_9, 'UTF-8, ASCII, ISO-8859-9');

  //echo $encoding;
  //echo $encoding_9." ";

  //echo  $adminType." ".$adminType_9;

  //echo $searchCrit;
  //echo $prov;

  $rowsPerPage = 20;
  $pageNum = 1;

  if(isset($_REQUEST['page']))
  {
      $pageNum = $_REQUEST['page'];
  }

  $offset = ($pageNum - 1) * $rowsPerPage;
  $self = $_SERVER['PHP_SELF'];



  $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER." password=".PG_PWD);

  if ($adminType == "province")
  {
		$sql = "select distinct iladi from iller";
		$result = pg_query($con, $sql);

		if (!$result) {
			echo "Problem with query " . $query . "<br/>";
			echo pg_last_error();
			exit();
		}

		$counter = 0;
		$cityListArr;
		$cityList;

		while($myrow = pg_fetch_assoc($result))
		{

			$counter+=1;
			$cityListArr[$counter] = htmlspecialchars($myrow['iladi']);

		}


		for ($i=1; $i <= sizeof($cityListArr); $i+=1)
		{
			$cityList = $cityList."--".$cityListArr[$i];
		}

		$cityList = substr_replace($cityList, "OK", 0,strlen("--"));

		echo $cityList;
  }
  else if ($adminType == "district")
  {
		$province = $_REQUEST["province"];
		$province_UTF = mb_convert_encoding($province, "UTF-8");

		$sql = "select distinct ilceadi from ilceler where iladi='".$province_UTF."'";
		$sql = turkishChars($sql);

		$result = pg_query($con, htmlspecialchars($sql));

		if (!$result) {
			echo "Problem with query " . $query . "<br/>";
			echo pg_last_error();
			exit();
		}

		$counter = 0;
		$districtListArr;
		$districtList;

		while($myrow = pg_fetch_assoc($result))
		{
			$counter+=1;
			$districtListArr[$counter] = htmlspecialchars($myrow['ilceadi']);
		}

		for ($i=1; $i <= sizeof($districtListArr); $i+=1)
		{
			$districtList = $districtList."--".$districtListArr[$i];
		}

		$districtList = substr_replace($districtList, "OK", 0,strlen("--"));

		echo $districtList;
  }
  else if ($adminType == "subdistrict")
  {
		$province = $_REQUEST["province"];
		$district = $_REQUEST["district"];

		$province_UTF = mb_convert_encoding($province, "UTF-8");
		$district_UTF = mb_convert_encoding($district, "UTF-8");

		$sql = "select distinct adi from mahalle_all where iladi='".$province_UTF."' and ilceadi='".$district_UTF."'";
		$sql = turkishChars($sql);

		//echo "Sql: ".$sql;

		$result = pg_query($con, htmlspecialchars($sql));

		if (!$result) {
			echo "Problem with query " . $query . "<br/>";
			echo pg_last_error();
			exit();
		}

		$counter = 0;
		$subdistrictListArr;
		$subdistrictList;

		while($myrow = pg_fetch_assoc($result))
		{
			$counter+=1;
			$subdistrictListArr[$counter] = htmlspecialchars($myrow['adi']);
		}

		for ($i=1; $i <= sizeof($subdistrictListArr); $i+=1)
		{
			$subdistrictList = $subdistrictList."--".$subdistrictListArr[$i];
		}

		$subdistrictList = substr_replace($subdistrictList, "OK", 0,strlen("--"));

		echo $subdistrictList;
  }
  else if ($adminType == "")
  {
		$sqlCrit = "";
		$sql = "";
		$sqlCount = "";

		if(isset($_REQUEST['prov']))
		{
		      $prov = mb_convert_encoding($_REQUEST['prov'], "UTF-8");
		      $sqlCrit = " city = '".$prov."'";
  	    }

  	    if(isset($_REQUEST['dist']))
		{
			  $dist = mb_convert_encoding($_REQUEST['dist'], "UTF-8");
			  $sqlCrit = $sqlCrit." and district = '".$dist."'";
  	    }

  	    if(isset($_REQUEST['subdist']))
		{
			  $subdist = mb_convert_encoding($_REQUEST['subdist'], "UTF-8");
			  $sqlCrit = $sqlCrit." and subdistric = '".$subdist."'";
  	    }

  	    if(isset($_REQUEST['searchCrit']))
		{
			  $searchCrit = mb_convert_encoding($_REQUEST['searchCrit'], "UTF-8");
			  $sqlCrit = $sqlCrit." and adi ilike '%".$searchCrit."%'";
  	    }

  		if(isset($_REQUEST['searchType']))
  		{
			$searchType = $_REQUEST['searchType'];
			if ($searchType == 'addressFind')
			{
				$sql = "select gid, adi, city from yolorta_all_wadm where".$sqlCrit;
  				$sqlCount = "select count(*) from yolorta_all_wadm where".$sqlCrit;

			}
			else if ($searchType == 'poiFind')
			{
				$sql = "select gid, adi, city from donati_all where".$sqlCrit;
				$sqlCount = "select count(*) from donati_all where".$sqlCrit;
			}
		}

		//count stuff
		//*--*

		$sqlCount = turkishChars($sqlCount);

		$resultCount = pg_query($con, $sqlCount);

		while ($countrow = pg_fetch_assoc($resultCount))
		{
			$rowCount = $countrow['count'];
		}

		$maxPage = ceil($rowCount/$rowsPerPage);

		//*--*

		$sql = $sql." limit ".$rowsPerPage." offset ".$offset;
		echo $sql;
		$sql = turkishChars($sql);

		$result = pg_query($con, htmlspecialchars($sql));

		echo "Count: ".$rowCount;


		if (!$result)
		{
			echo "Problem with query " . $query . "<br/>";
			echo pg_last_error();
			exit();
		}

		$resultStr = $resultStr."<table border='1' width='100%' cellpadding='1' id='resultTbl'>";
		$resultStr = $resultStr."<tr>";
		$resultStr = $resultStr."<td width='70%' valign='top'>Yol Ad&#305</td>";
		$resultStr = $resultStr."<td width='30%' valign='top'>Haritada G&#246ster</td>";
		while($myrow = pg_fetch_assoc($result))
		{
			$resultStr = $resultStr."<tr>";
			$resultStr = $resultStr."<td>".htmlspecialchars($myrow['adi'])." / ".htmlspecialchars($myrow['city'])."</td>";
			$resultStr = $resultStr."<td>";
			$resultStr = $resultStr."<input type='image' src='/img/zoom-in.png' name='TblZoomIn' id='g_".htmlspecialchars($myrow['gid'])."' onclick='getZoomIn(".htmlspecialchars($myrow['gid']).",&#39".$searchType."&#39);'>";
			$resultStr = $resultStr."</td>";
			$resultStr = $resultStr."</tr>";
		}
		$resultStr = $resultStr."</tr>";
		$resultStr = $resultStr."</table>";


		//paging stuff
		//*--*

		if ($pageNum > 1)
		{
		   $page  = $pageNum - 1;
		   $prev  = "<a href='javascript:dbPaging(".$page.")'>[&#214nceki]</a>";

		   $first = "<a href='javascript:dbPaging(1)'>[&#304lk Sayfa]</a>";
		}
		else
		{
		   $prev  = '&nbsp;'; // we're on page one, don't print previous link
		   $first = '&nbsp;'; // nor the first page link
		}


		if ($pageNum < $maxPage)
		{
		   $page = $pageNum + 1;
		   $next = "<a href='javascript:dbPaging(".$page.")'>[Sonraki]</a>";

		   $last = "<a href='javascript:dbPaging(".$maxPage.")'>[Son Sayfa]</a>";
		}
		else
		{
		   $next = '&nbsp;'; // we're on the last page, don't print next link
		   $last = '&nbsp;'; // nor the last page link
		}

		$resultStr = $resultStr. $first . $prev ." " .$pageNum. " / ".$maxPage. " "  . $next . $last;

		//*--*

		$resultStr_UTF = mb_convert_encoding($resultStr, "ISO-8859-9");

		echo $resultStr_UTF;
  }
  else if ($showIcons == "1")
  {
		$handle = opendir ('.');
		 while (false !== ($file = readdir($handle)))
		 {
					if($file != "." && $file != ".." && $file != basename(__FILE__))
					{
							echo '<img src="/img/'.$file.'"/>'.$file.'';
					}
			}

	}

  function turkishChars($txt)
  {
	  $txt = str_replace("&#351", "' || chr(351) || '", $txt);
	  $txt = str_replace("&#304", "' || chr(304) || '", $txt);
	  $txt = str_replace("&#350", "' || chr(350) || '", $txt);
	  $txt = str_replace("&#286", "' || chr(286) || '", $txt);
	  $txt = str_replace("&#305", "' || chr(305) || '", $txt);
	  $txt = str_replace("&#287", "' || chr(287) || '", $txt);

	  //echo $txt;
	  return $txt;
  }

pg_close($con);

?>